width = room_width/16
height = room_height/16
num = argument[0]
town_x[1] = 0
town_y[1] = 0
global.road_paths = ds_map_create()
for (var t=1;t<=round(num/3);t+=1)
    {
    var safe = false
    while (safe = false)
        {
        safe = true
        n = irandom_range(0,global.town_sites_num-1)
        junction_x = global.town_sites[n,1]
        junction_y = global.town_sites[n,2]
        //town_x[t] = irandom_range(3,width-3)
        //town_y[t] = irandom_range(3,height-3)
        //bck = instance_place(town_x[t],town_y[t],obj_back)
        for (var i=0;i<instance_number(obj_junction);i+=1)
            {
            obj = instance_find(obj_junction,i)
            if point_distance(obj.x,obj.y,junction_x,junction_y) < 500
                {
                safe = false
                }
            }
        if instance_number(obj_junction) = 0
            safe = true
        }
    instance_create(junction_x+(global.back_tile_dim/2),junction_y+(global.back_tile_dim/2),obj_junction)
    //show_debug_message('width set to ' + string(town_x[t]) + ' and height set to ' + string(town_y[t]))
    }
    
for (var t=0;t<instance_number(obj_junction);t+=1)
    {
    if (t+1) < instance_number(obj_junction)
        {
        p = instance_find(obj_junction,t)
        d = instance_find(obj_junction,t+1)
        r = instance_create(p.x,p.y,obj_roadpather)
        r.target = d
        r.start = p
        }
    else
        {
        p = instance_find(obj_junction,t)
        d = instance_find(obj_junction,0)
        r = instance_create(p.x,p.y,obj_roadpather)
        r.target = d
        }
    }

for (var t=1;t<num;t+=1)
    {
    var safe = false
    while (safe = false)
        {
        safe = true
        n = irandom_range(0,global.town_sites_num-1)
        town_x[t] = global.town_sites[n,1]
        town_y[t] = global.town_sites[n,2]
        //town_x[t] = irandom_range(3,width-3)
        //town_y[t] = irandom_range(3,height-3)
        bck = instance_place(town_x[t],town_y[t],obj_back)
        for (var i=0;i<instance_number(obj_townseed);i+=1)
            {
            obj = instance_find(obj_townseed,i)
            dist_t = point_distance(obj.x,obj.y,town_x[t],town_y[t])
            if dist_t < 500
                {
                safe = false
                }
            }
        if instance_number(obj_townseed) = 0
            safe = true
        }
    b = instance_create(town_x[t]+(global.back_tile_dim/2),town_y[t]+(global.back_tile_dim/2),obj_townseed)
    with (b)
        {
        r = instance_create(x,y,obj_roadpather)
        r.target = instance_nearest(x,y,obj_junction)
        r.start = self
        
        } 
    //show_debug_message('width set to ' + string(town_x[t]) + ' and height set to ' + string(town_y[t]))
    }

/*for (var i=1;i<num;i+=1)
    {
    d = 0
    while (d <= 3)
        {
        target = instance_find(obj_townseed,i)
            r = instance_create(town_x[i],town_y[i],obj_roadpather)
            r.target = target
            t.destinations[i] = r.target
        }
    //tile_add(bck_road,0,0,16,16,town_x[i]*16,town_y[i]*16,1)*/
    //}
//show_debug_message('width set to ' + string(width) + ' and height set to ' + string(height))

/*for (var i=0;i<=width;i+=1)
    {
    for (var j=0;j<=height;j+=1)
        {
        tile_add(bck_grass,0,0,16,16,i*16,j*16,2)
        num+=1
        show_debug_message('tile #'+string(num)+ ' created at (' + string(i*16) + ',' +string(j*16) + ')')
        }
    }
